home *** CD-ROM | disk | FTP | other *** search
- Path: news.polymtl.ca!news
- From: Pierre Ferland <pierre@meca.polymtl.ca>
- Newsgroups: comp.lang.c++
- Subject: Re: Multiple Inheritance Pointer Problem
- Date: Wed, 03 Apr 1996 15:44:58 -0500
- Organization: Center for Applied Research on Polymers
- Message-ID: <3162E34A.15FB@meca.polymtl.ca>
- References: <31622A26.3633@nmaa.org> <s8k9zxo51p.fsf_-_@socrates.ansa.co.uk>
- NNTP-Posting-Host: crasp.meca.polymtl.ca
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; AIX 2)
-
- Yes, right Toby. One may also try this:
-
- class Base_c { };
- class Derived_c : public Base_c {};
-
- int main(int, char *[]) {
-
- Derived_c derived;
-
- Derived_c *ptr_derived = &derived;
- Base_c *ptr_base = (Base_c *)ptr_derived;
- void *ptr_void = (void *) ptr_derived;
-
- printf("\n%x %x %x", ptr_derived, ptr_base, ptr_void);
-
- }
-
- The type cast from Derived_c * to Base_c * changes the address,
- so that the pointer actually points inside a Base_c object
- (the Base_c part of Derived_c)
-
- The type cast to void * obviously leaves the pointer unchanged.
- I recommend that you NEVER hard typecast a derived class
- into a void (example in a virtual constructor). Even though
- it sounds obvious many beginners do the fatal mistake, and are
- brought into a land of bizarre bugs...
-
- Pierre
-